package com.example.studywords.dlgs;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.DialogFragment;

import com.example.studywords.R;
import com.example.studywords.utils.SqlUtil;

public class AddWordDlg extends DialogFragment {

    public Dialog onCreateDialog(Bundle savedInstanceState){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("添加单词");
        LayoutInflater inflater = getActivity().getLayoutInflater();
        @SuppressLint("InflateParams")
        View view = inflater.inflate(R.layout.dlg_add_word, null);
        builder.setView(view);

        builder.setNegativeButton("取消",((dialogInterface, i) -> {

        }));

        builder.setPositiveButton("完成",((dialogInterface, i) -> {
            TextView wordText = view.findViewById(R.id.wordTextAddDlg),
            translateText = view.findViewById(R.id.translateTextAddDlg),
            noteText = view.findViewById(R.id.noteTextAddDlg);
            Context context = getActivity();

            new Thread(()->{
                SqlUtil.insertRelation(wordText.getText().toString(), translateText.getText().toString(),
                        noteText.getText().toString(),context);
            }).start();
        }));

        return builder.create();
    }
}
